package model

import (
	"stock/global"

	"gorm.io/gorm"
)

type PurchaseModel struct {
	Base
}

type MallPurchase struct {
	Id             uint   `json:"id" gorm:"<-:false; primaryKey;column:id;type:int(11)"`
	Logo           string `json:"logo" gorm:"column:logo;"`
	CountryCode    string `json:"country_code" gorm:"<-:false; column:country_code;"`
	ContactPhone   string `json:"contact_phone" gorm:"column:contact_phone;"`
	Status         int    `json:"status" gorm:"column:status;"`
	UserId         uint   `json:"user_id" gorm:"<-:false; column:user_id;"`
	ExpirationDate uint   `json:"expiration_date" gorm:"column:expiration_date;"`
	CheckState     uint8  `json:"check_state" gorm:"column:check_state;"`
	Email          string `json:"email" gorm:"column:email;"`
	Perfect        string `json:"perfect" gorm:"column:perfect;"`
	PurchaseLang   string `json:"lang" gorm:"column:lang;"`
	CreateAt       string `json:"create_at" gorm:"<-:false; column:create_at;"`
	UpdatedAt      string `json:"updated_at" gorm:"column:updated_at;"`
}

type RequestPurchase struct {
	CountryCode string
	Perfect     []string
	UserId      uint
	Status      uint8
	CheckState  uint8
	IdArr       []uint
}

type ResponsePurchase struct {
	MallPurchase
	ResponseLang
	ResponseFile
	MerchantId uint
}

func (m *PurchaseModel) TabName() string {
	return global.CONF.Mysql.Prefix + "mall_purchase_order"
}

// 列表
func (m *PurchaseModel) List(param *RequestPurchase, page global.PageSort) ([]ResponsePurchase, error) {

	where := &MallPurchase{}
	if param.CheckState != 0 {
		where.CheckState = param.CheckState
	}
	var ResponsePurchase []MallPurchase

	var tx *gorm.DB
	tx = global.DB.Debug().Where(where)
	if len(param.IdArr) > 0 && len(param.Perfect) > 0 {
		tx = tx.Where("id IN ?", param.IdArr).Where("perfect IN ?", param.Perfect)
	} else if len(param.IdArr) > 0 {
		tx = tx.Where("id IN ?", param.IdArr)
	} else if len(param.Perfect) > 0 {
		tx = tx.Where("perfect IN ?", param.Perfect)
	}
	tx.Order(page.Sortfield + " " + page.Sort).Offset(page.Page).Limit(page.Limit).Find(&ResponsePurchase)
	if tx.Error != nil {
		return nil, tx.Error
	}
	// var resPurOrderArr []ResponsePurchase
	// purchaseOrderLen := len(ResponsePurchase)
	// if purchaseOrderLen > 0 {

	// 	var purchaseOrderLangModel *LangModel = new(LangModel)
	// 	var fileModel *FileModel = new(FileModel)

	// 	var purchaseOrderWg sync.WaitGroup
	// 	purchaseOrderWg.Add(purchaseOrderLen)
	// 	for i := 0; i < purchaseOrderLen; i++ {

	// 		go func(tabName string, tableId uint, purOrder MallPurchase, wg *sync.WaitGroup) {

	// 			var resPurOrder ResponsePurchase
	// 			var purchaseOrderLang ResponseLang
	// 			purchaseOrderLang, _ = purchaseOrderLangModel.List(RequestLang{
	// 				TableName: tabName,
	// 				TableId:   tableId,
	// 				Lang:      global.LANG_COUNTRY.Lang,
	// 				Field:     []string{"product_name", "purchase_detail"},
	// 			})
	// 			resPurOrder.ProductName = purchaseOrderLang.ProductName
	// 			resPurOrder.ExpirationDate = purOrder.ExpirationDate

	// 			//FILE
	// 			var reFile ResponseFile
	// 			reFile, _ = fileModel.List(RequestFile{
	// 				MerchantType: 2,
	// 				Lang:         global.LANG_COUNTRY.Lang,
	// 				MerchantId:   ResponsePurchase[i].PurchaseId,
	// 				ProductId:    ResponsePurchase[i].Id,
	// 				UsageTypd:    []uint{4, 7, 8, 10, 13},
	// 			})
	// 			resPurOrder.PurchaseOrderCover = reFile.MainImg
	// 			resPurOrderArr = append(resPurOrderArr, resPurOrder)

	// 			wg.Done()
	// 		}(m.TabName(), ResponsePurchase[i].Id, ResponsePurchase[i], &purchaseOrderWg)
	// 	}
	// 	purchaseOrderWg.Wait()
	// }
	return nil, nil
}

// 基础详情
func (m *PurchaseModel) BaseDetail() {

}

// 详情
func (m *PurchaseModel) Detail(param RequestPurchase, page global.PageSort) (ResponsePurchase, error) {

	where := &MallPurchase{}
	if param.CountryCode != "" {
		where.CountryCode = param.CountryCode
	}

	if param.UserId != 0 {
		where.UserId = param.UserId
	}
	if param.CheckState != 0 {
		where.CheckState = param.CheckState
	}
	var supplier MallPurchase
	var supplierLang ResponsePurchase
	var tx *gorm.DB
	tx = global.DB.Debug().Where(where)
	if len(param.IdArr) > 0 && len(param.Perfect) > 0 {
		tx = tx.Where("id IN ?", param.IdArr).Where("perfect IN ?", param.Perfect)
	} else if len(param.IdArr) > 0 {

		tx = tx.Where("id IN ?", param.IdArr)
	} else if len(param.Perfect) > 0 {
		tx = tx.Where("perfect IN ?", param.Perfect)
	}
	tx.Order(page.Sortfield + " " + page.Sort).First(&supplier)
	if supplier.Id != 0 {

		var supplierLangModel *LangModel = new(LangModel)
		var reLang ResponseLang
		reLang, _ = supplierLangModel.List(RequestLang{
			TableName: m.TabName(),
			TableId:   supplier.Id,
			Lang:      global.LANG_COUNTRY.Lang,
			Field:     []string{"description", "main_product", "address", "supplier_name"},
		})
		supplierLang.MerchantName = reLang.MerchantName
		if reLang.Description != "" {
			supplierLang.Description = reLang.Description
		} else if reLang.Description == "" && reLang.Describe != "" {
			supplierLang.Description = reLang.Describe
		}
		supplierLang.Logo = supplier.Logo
		supplierLang.Address = reLang.Address
		supplierLang.MainProduct = reLang.MainProduct
		supplierLang.Contactname = reLang.Contactname
	}
	return supplierLang, nil
}
